package com.tedu.aopdemo.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

import java.util.Arrays;

/**
 * 定义一个切面类，用于在项目业务逻辑中动态添加逻辑
 */
@Component//需要被spring管理
@Aspect//切面类
public class SysOutAspect {
    /**
     *
     * @param joinPoint
     */
    @Before("execution(public int com.tedu.aopdemo.aop.CalculatorImpl.*(..))")
    public void before(JoinPoint joinPoint){
        System.out.println("target"+joinPoint.getTarget());//获取切入点方法所在的对象
        System.out.println(joinPoint.getSignature().getName());//方法名
        System.out.println(Arrays.toString(joinPoint.getArgs()));//实际参数

        String className = joinPoint.getTarget().getClass().getSimpleName();
        String methodName =  joinPoint.getSignature().getName();
        String args = Arrays.toString(joinPoint.getArgs());
        System.out.println("开始调用"+className+"."+methodName+"方法，实参为"+args);
        System.out.println();
        System.out.println("该切面逻辑在方法之前被调用了");
    }

    @AfterReturning(value = "execution(public int com.tedu.aopdemo.aop.CalculatorImpl.*(..))",
    returning = "returnValue")
    public void afterReturning(JoinPoint joinPoint,Object returnValue){

        String className = joinPoint.getTarget().getClass().getSimpleName();
        String methodName = joinPoint.getSignature().getName();
        System.out.println(className+"."+methodName+"执行完毕，返回值"+returnValue);
    }

    @After("execution(public int com.tedu.aopdemo.aop.CalculatorImpl.*(..))")
    public void after(JoinPoint joinPoint){
        String className = joinPoint.getTarget().getClass().getSimpleName();
        String methodName = joinPoint.getSignature().getName();
        System.out.println(className+"."+methodName+"()方法执行完毕");
    }
    @AfterThrowing(value = "execution(public int com.tedu.aopdemo.aop.CalculatorImpl.*(..))",throwing = "e")
    //throwing="e":指定将实际方法抛出的异常传递到下面方法的哪个参数
    public void afterThrowing(JoinPoint joinPoint,Exception e){
        String className = joinPoint.getTarget().getClass().getSimpleName();
        String methodName = joinPoint.getSignature().getName();
        System.out.println(className+"."+methodName+"()方法抛出异常"+e.getMessage());
    }



    @After("execution(public String com.tedu.aopdemo.aop.CalculatorImpl.*(..))")
    public void after1(JoinPoint joinPoint){
        String className = joinPoint.getTarget().getClass().getSimpleName();
        String methodName = joinPoint.getSignature().getName();
        System.out.println(className+"."+methodName+"()String方法执行完毕");
    }

    /*@Around("execution(public int com.tedu.aopdemo.aop.CalculatorImpl.*(..))")
    public Object around(ProceedingJoinPoint joinPoint) throws  Throwable{
        String className = joinPoint.getTarget().getClass().getSimpleName();
        String methodName = joinPoint.getSignature().getName();
        String args  = Arrays.toString(joinPoint.getArgs());
        System.out.println(className+"."+methodName+"()方法执行，参数为"+args);


        try {
            Object returnValue = joinPoint.proceed();
            System.out.println(className+"."+methodName+"()方法执行完毕");
            return returnValue;
        } catch (Throwable e) {
            System.out.println(className+"."+methodName+"()方法出现异常："+e.getMessage());
            throw new RuntimeException(e);
        } finally {

            System.out.println(className+"."+methodName+"()方法执行完毕");
        }

    }*/
}
